package vn.mobiistar.videoapp.fragment;

import java.util.ArrayList;

import vn.mobiistar.videoapp.R;
import vn.mobiistar.videoapp.activity.MainActivity;
import vn.mobiistar.videoapp.loader.BaseResult;
import vn.mobiistar.videoapp.loader.CategoryLoader;
import vn.mobiistar.videoapp.other.Category;
import vn.mobiistar.videoapp.other.Define;
import vn.mobiistar.videoapp.other.ImageLoad;
import vn.mobiistar.videoapp.other.VideoPlayer;
import vn.mobiistar.videoapp.ui.EmptyLoadingView;
import vn.mobiistar.videoapp.ui.Refreshable;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MenuLeftFragment extends BaseFragment implements LoaderCallbacks<BaseResult>, Refreshable{
	private Context mContext;
	protected ArrayList<Category> mData;
	protected boolean mIsReachedBottom  =false;
	public CategoryLoader mLoader;
	public EmptyLoadingView mLoading;
	private ListView mList;
	private MenuAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreateView(inflater, container, savedInstanceState);
		
		View menuContent = inflater.inflate(R.layout.sliding_menu_left, null);
		mContext = getActivity();
		mList = (ListView) menuContent.findViewById(R.id.llListMenu);
		mLoader = new CategoryLoader(mContext);
		mLoader.setType(Define.SERVICE_GET_CATEGORYFORMENU);
		mData = new ArrayList<Category>();
		mLoading = (EmptyLoadingView) menuContent.findViewById(R.id.ll_loading);
		
		((MainActivity)getActivity()).currentFragment = this;
		
		mList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				CategoryVideoListFragment listVideo = CategoryVideoListFragment.getInstance(mContext, mData, position, null,null);
				if (((MainActivity)mContext).savedInstanceState == null) {
					FragmentTransaction transaction = getFragmentManager().beginTransaction();
					transaction.replace(R.id.ma_container, listVideo);
					// Commit the transaction
					transaction.commit();
				}
				((MainActivity)mContext).navSearchButton.setVisibility(View.VISIBLE);
				((MainActivity)mContext).navSearchText.setVisibility(View.GONE);
				((MainActivity)mContext).navSearchRemoveButton.setVisibility(View.GONE);
				((MainActivity)mContext).setTitle(((Category) ((MenuAdapter) ((ListView) parent).getAdapter()).getItem(position)).getName());
				((MainActivity)mContext).showContent();
			}
		});
		
		return menuContent;
	}
	
	public void setAdapter(){
		mAdapter = new MenuAdapter();
		mList.setAdapter(mAdapter);
		resetListHeight();
		
	}
	private void resetListHeight(){
		int totalHeight = 0;//it is the ListView Height
		for (int i = 0, len = mAdapter.getCount(); i < len; i++) {
		    View listItem = mAdapter.getView(i, null, mList);
		    listItem.measure(0, 0);
		    int list_child_item_height = listItem.getMeasuredHeight()+mList.getDividerHeight();//item height
		    totalHeight += list_child_item_height; //
		}
		mList.getLayoutParams().height = totalHeight;
	}
	
	private class MenuAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return MenuLeftFragment.this.mData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return MenuLeftFragment.this.mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View v = convertView;
			if (v == null) {
				LayoutInflater vi;
				vi = LayoutInflater.from(mContext);
				v = vi.inflate(R.layout.slidemenu_item, null);
			}
			TextView name = (TextView) v.findViewById(R.id.si_text);
			name.setText(MenuLeftFragment.this.mData.get(position).getName());
			ImageView icon = (ImageView) v.findViewById(R.id.si_icon);
			
			String url = mData.get(position).getIcon();
			Log.v("image url",url);
			ImageLoad.display(url, icon);
			
			ImageView type = (ImageView) v.findViewById(R.id.si_typeicon);
			if(mData.get(position).getIsFeature() == Define.CATETYPE_NEW){
				type.setImageResource(R.drawable.ic_menu_new);
			}else
			if(mData.get(position).getIsFeature() == Define.CATETYPE_HOT){
				type.setImageResource(R.drawable.ic_menu_hot);
			}
			return v;
		}
		
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		getLoaderManager().initLoader(Define.SERVICE_GET_CATEGORYFORMENU, null, this);
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public Loader<BaseResult> onCreateLoader(int id, Bundle arg1) {
		mLoader.setProgressNotifiable( mLoading);
		return mLoader;
	}

	@Override
	public void onLoadFinished(Loader<BaseResult> loader,final BaseResult result) {
		// TODO Auto-generated method stub
		final int id = loader.getId();
		((MainActivity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if(id == Define.SERVICE_GET_CATEGORYFORMENU) if (((CategoryLoader.Result) result).mCategory.size() > 0) {
					mData = ((CategoryLoader.Result) result).mCategory;
					// set adapter de hien thi danh sach cac tab tren thanh tabhost
					setAdapter();
				} else
					mIsReachedBottom = true;
			}
		});
	}

	@Override
	public void onLoaderReset(Loader<BaseResult> arg0) {
	}

	@Override
	public void refreshData() {
	}
	
}
